package com.spark.lola.configuration;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.spark.lola.support.api.configuration.JwtProperties;
import com.spark.lola.support.api.configuration.TokenInterceptor;
import com.spark.lola.support.api.entity.EnterpriseTokenUser;
import com.spark.lola.support.api.service.JwtService;
import com.spark.lola.support.api.util.ConfigUtil;
import com.spark.lola.support.common.entity.ShiroUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.lang.NonNull;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * MVC自定义配置
 * @Author: DengYeJun
 * @Date: 2019/12/31 0031 19:03
 */
@Configuration
public class MvcConfiguration implements WebMvcConfigurer {

    private final TokenInterceptor<?> tokenInterceptor;

    @Value("${swagger.enable}")
    private boolean swaggerEnable;

    @Autowired
    public MvcConfiguration(JwtProperties jwtProperties, JwtService jwtService) {
        this.tokenInterceptor = new TokenInterceptor<>(jwtProperties, jwtService, EnterpriseTokenUser.class);
    }

    @Bean
    public TokenInterceptor<?> tokenInterceptor() {
        return this.tokenInterceptor;
    }

    @Bean
    public SimpleControllerHandlerAdapter simpleControllerHandlerAdapter() {
        return new SimpleControllerHandlerAdapter();
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 移除系统 内置MappingJackson2HttpMessageConverter
        converters.removeIf(converter -> converter instanceof MappingJackson2HttpMessageConverter);

        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        final ObjectMapper objectMapper = new ObjectMapper();
        /**
         * 序列换成json时,将所有的long变成string
         * 因为js中得数字类型不能包含所有的java long值
         */
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
        simpleModule.addSerializer(Enum.class,ToStringSerializer.instance);

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        objectMapper.setDateFormat(formatter);
        objectMapper.registerModule(simpleModule);
        jackson2HttpMessageConverter.setObjectMapper(objectMapper);
        //添加字节转化
        converters.add(jackson2HttpMessageConverter);
    }


    @Override
    public void addInterceptors(@NonNull InterceptorRegistry registry) {
        // 设置用户鉴权拦截器&过滤路径
        List<String> excludePaths =new ArrayList<>();
        excludePaths.add("/api/huarong/auth/**");
        if(swaggerEnable){
            excludePaths.add("/swagger-ui.html");
            excludePaths.add("/swagger-resources/**");
            excludePaths.add("/v2/**");
            excludePaths.add("/webjars/**");
        }
        registry.addInterceptor(tokenInterceptor).addPathPatterns("/api/**")
                .excludePathPatterns(excludePaths);
    }

}